package com.orangelife.mobile.goodsrent.adapter;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.util.DateUtil;
import com.orangelife.mobile.util.StringUtil;

import java.util.List;
import java.util.Map;

public class GoodsRentStatesListAdapter<T> extends FatherAdapter<T> {

	private Context context;
	private List<Map<String, Object>> list;
	private Map<String, Object> infomap;
	private LayoutInflater inflater;
	private ViewHolder holder;

	private Dialog dialog;


	public GoodsRentStatesListAdapter(Context context,
									  List<Map<String, Object>> list) {
		this.context = context;
		this.list = list;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		if (list != null) {
			return list.get(position);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int posiiton, View convertView, ViewGroup parent) {
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_lv_goodsrent_states, null);
			holder.ivBall = (ImageView) convertView.findViewById(R.id.ivBall);
			holder.tvStates = (TextView) convertView.findViewById(R.id.tvStates);
			holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
//			holder.tvOverrude = (TextView) convertView.findViewById(R.id.tvOverrude);
			holder.viewButtom = convertView.findViewById(R.id.viewButtom);
			holder.viewTop = convertView.findViewById(R.id.viewTop);
			holder.llText = (LinearLayout) convertView.findViewById(R.id.llText);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		infomap = list.get(posiiton);
		if(posiiton == 0){
			holder.viewTop.setVisibility(View.GONE);
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(8,0,0,0);
			holder.llText.setLayoutParams(layoutParams);
			holder.ivBall.setBackgroundResource(R.drawable.goodsrent_ok);
		}else{
			holder.viewTop.setVisibility(View.VISIBLE);
		}
		if(posiiton == list.size() - 1){
			holder.viewButtom.setVisibility(View.INVISIBLE);
			/*RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(0,0,0,30);
			holder.ivBall.setLayoutParams(layoutParams);*/
		}else{
			holder.viewButtom.setVisibility(View.VISIBLE);
		}
		String orderStatus = infomap.get("orderStatus").toString();
		String orderStatusStr = "";
		String time = infomap.get("tscreate").toString();
		String remark = infomap.get("remark").toString();
		if(StringUtil.isBlank(remark)){
			holder.tvTime.setText(DateUtil.getInstance().tranStringForDate(time));
		}else {
			holder.tvTime.setText(infomap.get("remark").toString());
		}
		switch (orderStatus) {
			case "0":
				orderStatusStr = "待付款";
				break;
			case "1":
				orderStatusStr = "已付款";
				break;
			case "2":
				orderStatusStr = "待发货";
				break;
			case "3":
				orderStatusStr = "已发货";
				break;
			case "7":
				orderStatusStr = "已逾期";
				holder.tvTime.setText(DateUtil.getInstance().tranStringForDate(time)+" "+infomap.get("remark").toString());
				break;
			case "4":
				orderStatusStr = "已归还";
				break;
			case "6":
				orderStatusStr = "已丢失";
				break;
			case "5":
				orderStatusStr = "已评价";
				break;

			default:
				break;
		}
		holder.tvStates.setText(orderStatusStr);


		return convertView;
	}







	class ViewHolder {
		/**圆圈点*/
		private ImageView ivBall;
		/**状态*/
		private TextView tvStates;
		/**更改状态的时间*/
		private TextView tvTime;
		/**过期（红色，gone）*/
//		private TextView tvOverrude;
		/**圆圈点下面的线*/
		private View viewButtom;
		/**圆圈点上的线*/
		private View viewTop;
		/**显示状态的LinearLayout*/
		private LinearLayout llText;
	}

	@Override
	public BaseAdapter setList(List<T> list) {
		this.list = (List<Map<String, Object>>) list;
		return GoodsRentStatesListAdapter.this;
	}

}
